import { ElMessage, ElMessageBox } from 'element-plus'
import { post } from "/src/api/index";
import { ref, unref, watch, onMounted } from 'vue'
import { getSelection, batchUpdateValue,fireExecute } from '/src/utils/index'

export function init(self) {
  const module = self.module.value
  self.custom = {
    columns : [
      {
        prop: 'workspace_name',
        order: 'asc',
        width: 150,
      },
      {
        prop: 'name',
        order: 'asc',
        width: 150,
      },
      {
        prop: 'tables',
        width: 400,
      },
      {
        prop: 'root_path',
        width: 300,
      }
    ],

    editColumns : [
      {
        prop: 'workspace_name',
        label: '工作区',
        list_remote: {
          module: 'sdp_workspace', method:'list',
          query: {},
          sortInfo: ['name'],
          mapInfo: {label:'name', value:'name'}
        },
        type: 'select',
        required: true,
        onchange: param => {
          let {mode, form, column, columns, get_remote_list, set_query_storage} = param
          if (mode.indexOf('init') < 0) {
          }
        },
      },
      {
        prop: 'name',
        required: true,
      },
      {
        prop: 'tables',
        type: 'textarea',
        rows: 8,
      }
    ],

    queryColumns : [
      {
        prop: 'workspace_name',
        fixed: true,
        list_remote: {
          module: 'sdp_workspace', method:'list',
          query: {},
          sortInfo: ['name'],
          mapInfo: {label:'name', value:'name'}
        },
        type: 'select',
      },
      {
        prop: 'name_like',
        label: '名称',
        width: '150px',
      },
      {
        prop: 'tables_like',
        label: '表',
        width: '150px',
      },
      {
        prop: 'root_path_like',
        label: '路径',
        width: '150px',
      },
    ],

    buttons: [
      {
        prop: 'fireExecute',
        label: '生成文件',
        loading: false,
        type: 'success',
        onclick: (param) => {
          fireExecute({param, self})
        }
      },
      {
        prop: 'setTables',
        label: '设置表及别名',
        loading: false,
        onclick: (param) => {
          let {setTablesDialogInfo : dialogInfo, selectRows} = self
          dialogInfo = unref(dialogInfo)
          selectRows = unref(selectRows)
          if (!selectRows || selectRows.length == 0) {
            ElMessage.error(`请选择至少一条记录后再操作`);
            return;
          }

          selectRows = unref(selectRows)
          let item = selectRows[0]
          post('sdp_workspace', 'get_table_list', {workspace_name: item.workspace_name})
          .then( data => {
            let notInclude = 0
            dialogInfo.columns = data.map(ele => {
              if (notInclude) {
                item[ele.name+'_active'] = 0
                return {
                  prop: ele.name,
                  label: ele.remark || ele.name,
                  type: 'project_table',
                }
              }
              if (ele.name.indexOf('tables not include') >= 0) {
                notInclude = 1
              } else {
                item[ele.name+'_active'] = 1
                if (ele.alias_name) {
                  item[ele.name+'_alias'] = ele.alias_name
                }
              }
              return {
                prop: ele.name,
                label: ele.remark || ele.name,
                type: 'project_table',
                active: 1,
              }
            }).filter(ele => ele.prop.indexOf('tables not include') < 0)
            dialogInfo.form = {...item}
            dialogInfo.form_ori = {...item}
            dialogInfo.show({...param, dialogInfo})
          })
        }
      },
      {
        prop: 'setRootPath',
        label: '设置(root)目录',
        loading: false,
        onclick: (param) => {
          let {editDialogInfo : dialogInfo, selectRows} = self
          selectRows = unref(selectRows)
          if (!selectRows || selectRows.length == 0) {
            let query = unref(self.query)
            if (query.workspace_name) {
              selectRows = unref(self.tableData)
            }
          }
          if (!selectRows || selectRows.length == 0) {
            ElMessage.error(`请选择至少一条记录后再操作`);
            return;
          }

          let item = selectRows[0]
          let foundItem = selectRows.filter(ele => ele.root_path.indexOf('(root)') >= 0)
          if (!foundItem || !foundItem.length) {
            ElMessage.error(`请选择至少一条未设置root目录的记录后再操作`);
            return;
          }

          ElMessageBox.prompt('请输入(root)目录', '设置(root)目录', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            closeOnPressEscape:false,
            closeOnClickModal:false,
            inputValidator: (val) => {
              if (val === null||val.length < 1) {
                return false;
              }
            },
            inputErrorMessage: '此项不能为空',
          })
            .then(({ value }) => {
              if (!value) {
                return;
              }
              while (value.substring(value.length - 1) == '/') {
                value = value.substring(0, value.length - 1)
              }
              while (value.substring(value.length - 1) == '\\') {
                value = value.substring(0, value.length - 1)
              }
              if (!value) {
                return;
              }
              dialogInfo.loading = true
              let count = 0
              foundItem.forEach( ele => {
                let form = {id:ele.id, root_path:ele.root_path.replace('(root)', value)}
                post('sdp_project', 'update', form)
                .then((data) => {
                  count++
                  if (count == foundItem.length) {
                    dialogInfo.loading = false
                    ElMessage.success('更新成功')
                    self.handleSearch()
                  }
                })
                .catch((err) => {
                  dialogInfo.loading = false
                  ElMessage.error(`无法更新，错误：${ex}`);
                })
              })
            })
            .catch(() => {
            })

        }
      },
    ],

    remoteConfig : {
    },

    onInit: () => {
      self.cellMouseEnter = (row, column, cell, event) => {
        if (column && column.property == 'tables') {
          // self.whiteSpace.value = 'pre-line'
        }
      }
      self.cellMouseLeave = (row, column, cell, event) => {
        // self.whiteSpace.value = 'nowrap'
      }
    },

  }
}

